package cn.yuanqiao.archive.appManager.service.impl;

import cn.yuanqiao.archive.appManager.domain.TenantApply;
import cn.yuanqiao.archive.appManager.mapper.TenantApplyMapper;
import cn.yuanqiao.archive.appManager.service.ITenantApplyService;
import cn.yuanqiao.common.core.domain.entity.SysRole;
import cn.yuanqiao.common.exception.ServiceException;
import cn.yuanqiao.common.utils.StringUtils;
import cn.yuanqiao.common.utils.SecurityUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * applymodelService业务层处理
 *
 * @author 赵常飞
 * @date 2023-06-16
 */
@Service
public class TenantApplyServiceImpl implements ITenantApplyService
{
    @Autowired
    private TenantApplyMapper tenantApplyMapper;

    /**
     * 查询applymodel
     *
     * @param menuId 菜单id
     * @param categoryId 门类id
     * @return applymodel
     */
    @Override
    public TenantApply selectYqdaApplyModelById(Long menuId, Long categoryId)
    {
        TenantApply tenantApply = new TenantApply();
        return tenantApplyMapper.selectYqdaApplyModelById(menuId,categoryId,tenantApply.getTableName());
    }

    /**
     * 查询applymodel列表
     *
     * @param yqdaApplyModel applymodel
     * @return applymodel
     */
    @Override
    public List<TenantApply> selectYqdaApplyModelList(TenantApply yqdaApplyModel)
    {
        return tenantApplyMapper.selectYqdaApplyModelList(yqdaApplyModel);
    }

    /**
     * 新增applymodel
     *
     * @param yqdaApplyModel applymodel
     * @return 结果
     */
    @Override
    public int insertYqdaApplyModel(TenantApply yqdaApplyModel)
    {
        String menuIds = yqdaApplyModel.getMenuIds();
        if(StringUtils.isNotEmpty(menuIds)){   //多模块添加
            String[] menuIdsArr = menuIds.split(",");
            Long categoryId = yqdaApplyModel.getCategoryId();
            int i1 = 0;
            int i2 = 0;
            for(int i=0;i<menuIdsArr.length;i++){
                TenantApply tenantApply = new TenantApply();
                tenantApply.setCategoryId(categoryId);
                tenantApply.setMenuId(Long.valueOf(menuIdsArr[i]));
                TenantApply tenantApply1 = tenantApplyMapper.selectYqdaApplyModelById(Long.valueOf(menuIdsArr[i]), categoryId, tenantApply.getTableName());
                if(tenantApply1!=null){
                    i2 = -1;
                    continue;
                }
                //i1 = tenantApplyMapper.insertTenantApply(yqdaApplyModel);
                //todo syd
                i1 = tenantApplyMapper.insertTenantApply(tenantApply);
            }
            if(i2!=0){
                return i2;
            }else{
                return i1;
            }
        }else{   //单模块添加 修改
            TenantApply tenantApply = new TenantApply();
            tenantApply.setMenuId(yqdaApplyModel.getMenuId());
            tenantApply.setCategoryId(yqdaApplyModel.getCategoryId());
            List<TenantApply> yqdaApplyModels = tenantApplyMapper.selectYqdaApplyModelList(tenantApply);
            if(yqdaApplyModels.size()==0){//没有相关数据则增加
                return tenantApplyMapper.insertTenantApply(yqdaApplyModel);
            }else{//存在则修改
                return tenantApplyMapper.updateYqdaApplyModel(yqdaApplyModel);
            }
        }

    }

    /**
     * 修改applymodel
     *
     * @param yqdaApplyModel applymodel
     * @return 结果
     */
    @Override
    public int updateYqdaApplyModel(TenantApply yqdaApplyModel)
    {
        return tenantApplyMapper.updateYqdaApplyModel(yqdaApplyModel);
    }

    /**
     * 批量删除applymodel
     *
     * @param categoryId 门类id
     * @param menuId 菜单id
     * @return 结果
     */
    @Override
    public int deleteYqdaApplyModel(Long categoryId,Long menuId)
    {
        TenantApply tenantApply = new TenantApply();
        return tenantApplyMapper.deleteYqdaApplyModel(categoryId,menuId,tenantApply.getTableName());
    }

    /**
     * 删除applymodel信息
     *
     * @param Id applymodel主键
     * @return 结果
     */
    @Override
    public int deleteYqdaApplyModelById(Long Id)
    {
        TenantApply tenantApply = new TenantApply();
        return tenantApplyMapper.deleteYqdaApplyModelById(Id,tenantApply.getTableName());
    }

    @Override
    public int clonefield(JSONObject json) {
        int i1 = 0;
        TenantApply tenantApply = new TenantApply();
        Long categoryId=json.getLong("categoryId");
        Long clonedmenuId=json.getLong("clonedmenuId");
        String clonetomenuIds1 = json.getString("clonetomenuIds");
        String[] clonetomenuIds = clonetomenuIds1.split(",");
        List<String> list  = new ArrayList<>();
        for(int i=0;i<clonetomenuIds.length;i++){
            String str = clonetomenuIds[i];
            TenantApply tenantApply1 = tenantApplyMapper.selectYqdaApplyModelById(Long.valueOf(str), categoryId, tenantApply.getTableName());
            if(tenantApply1!=null){
                list.add(str);
            }
        }
        if(list.size()==0){
            throw new ServiceException("模块未添加的不能复制");
        }
        if(clonetomenuIds.length!=list.size()){
            i1 = -1;
        }
        if(i1!=0){
            tenantApplyMapper.clonefield(categoryId,clonedmenuId,list,tenantApply.getTableName());
            return i1;
        }else{
            return tenantApplyMapper.clonefield(categoryId,clonedmenuId,list,tenantApply.getTableName());
        }
        //JSONArray clonetomenuIds=json.getJSONArray("clonetomenuIds");
    }

    @Override
    public List<Map<String, String>> selectYqdaApplyModelListmenu(String menuName,Long categoryId) {
        TenantApply tenantApply = new TenantApply();
        String tenantCode = SecurityUtils.getTenantCode();
        return tenantApplyMapper.selectYqdaApplyModelListmenu(menuName,tenantApply.getTableName(),tenantCode,categoryId);
    }

    @Override
    public List<Map<String, String>> listAllMenu(Long categoryId) {
        String tenantCode = SecurityUtils.getTenantCode();
        //String userRoleKey = SecurityUtils.getUserRoleKey();
        List<String> roleKey = new ArrayList<>();
        List<SysRole> roleList = SecurityUtils.getLoginUser().getUser().getRoles();
        for(SysRole sysRole:roleList){
            roleKey.add(sysRole.getRoleKey());
        }
        //String[] str = userRoleKey.split("\\|");
        //List<String> RoleKey = Arrays.asList(str);
        return tenantApplyMapper.listAllMenu(tenantCode,roleKey,categoryId,new TenantApply().getTableName());
    }

    @Override
    public String getfieldlist(JSONObject json) {
        TenantApply tenantApply = new TenantApply();
        return tenantApplyMapper.getfieldlist(json.getLong("menuId"),
                json.getLong("categoryId"),json.getString("field"),tenantApply.getTableName());
    }

    @Override
    public int deletebycatemenu(com.alibaba.fastjson.JSONObject json) {
        TenantApply tenantApply = new TenantApply();
        Long categoryId=json.getLong("categoryId");
        JSONArray menuIds=json.getJSONArray("menuIds");
        return tenantApplyMapper.deletebycatemenu(categoryId,menuIds,tenantApply.getTableName());
    }
}
